using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Mbs.Collections;
using Mbs.ComponentModel.Container;

namespace Mbs.Modularity
{
    
    public class ModuleMetaRepository : MarshalByRefObject, IModuleMetaRepository, IInitializable
    {
        readonly protected ICollection<IModuleMeta> _modules = new SyncList<IModuleMeta>();

        [Inject]
        public IModuleProvider ModuleProvider { get; set; }

        public ModuleMetaRepository()
        {
        }

        public virtual void Init() { }


        public virtual IModuleMeta[] GetModules()
        {
            return _modules.ToArray();
        }

        public IModuleMeta GetModule(string name)
        {
            return _modules.FirstOrDefault(moduleInfo => moduleInfo.Name == name);
        }

        public IModuleMeta Register(Assembly assembly)
        {
            var meta = ModuleProvider.CreateModuleMeta(assembly);
            _modules.Add(meta);
            return meta;
        }

        public IModuleMeta Register(string assemblyFile)
        {
            var meta = ModuleProvider.CreateModuleMeta(assemblyFile);
            _modules.Add(meta);
            return meta;
        }
    }
}
